package com.foreknow.elm.controller;

import com.foreknow.elm.po.User;
import com.foreknow.elm.service.UserService;
import com.foreknow.elm.service.impl.UserServiceImpl;

import javax.servlet.http.HttpServletRequest;

/**
 * 向用户表中添加一条记录
 * URL地址：
 * http://localhost:8080/elm/UserController/saveUser?userId=12345672222&userName=fender&password=123456&userSex=1
 */
public class UserController {
    public Object saveUser(HttpServletRequest request) {
        UserService userService = new UserServiceImpl();
        User user = new User();
        user.setUserId(request.getParameter("userId"));
        user.setUserName(request.getParameter("userName"));
        user.setPassword(request.getParameter("password"));
        user.setUserSex(Integer.parseInt(request.getParameter("userSex")));
        int result = userService.saveUser(user);
        return result;
    }

    /**
     * 根据用户编号查询用户表返回的行数
     * http://localhost:8080/elm/UserController/getUserById?userId=12345672222
     * @param reuqest
     * @return
     */
    public Object getUserById(HttpServletRequest reuqest) {
        UserService userService = new UserServiceImpl();
        int count = userService.getUserById(reuqest.getParameter("userId"));
        return count;
    }

    /**
     * http://localhost:8080/elm/UserController/getUserByIdByPass?userId=12345672222&password=123456
     * @param request
     * @return
     */
    public Object getUserByIdByPass(HttpServletRequest request) {
        UserService userService = new UserServiceImpl();
        String userId = request.getParameter("userId");
        String password = request.getParameter("password");
        User user = userService.getUserByIdByPass(userId,password);
        return user;
    }
}
